package Model;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.NumberFormat;
import java.util.ArrayList;

public class Mobile extends SanPham{
	private int mobileID;
	private String kichThuocManHinh;
	private String thongTinManHinh;
	//private String tinhNangNoiBat;
	private String bangTan;
	private String ngonNgu;
	private String cheDoRung;
	private String nhacChuong;
	private String loa;
	private String boNhoTrong;
	private String theNhoNgoai;
	private String gprs;
	private String edge;
	private String g3;
	private String wifi;
	private String bluetooth;
	private String hongNgoai;
	private String usb;
	private String camera;
	private HeDieuHanh heDieuHanh;
	private String cpu;
	private String sms;
	private String trinhDuyet;
	private String java;
	private String radio;
	private String music;
	private String video;
	private String office;
	private String tinhNangKhac;
	private String thoiGianThoai;
	private String thoiGianCho;
	private String phuKienDiKem;
	private String noiGiamGia;
	private String trangThai;
	
	public Mobile(){
		super();
	}
	
	
	public Mobile(String tenSP, int giaBan, HangSanXuat hangSanXuat,
			int soLuong, String hinhAnh, String anhDaiDien, Date ngayThang,
			int giaSauKhiGiam, String thongTinNoiBat,
			String thoiGianBaoHanh, String trongLuong, String kichThuoc,
			String mauSac, String kichThuocManHinh, String thongTinManHinh,
			String bangTan, String ngonNgu,
			String cheDoRung, String nhacChuong, String loa, String boNhoTrong,
			String theNhoNgoai, String gprs, String edge, String g3,
			String wifi, String bluetooth, String hongNgoai, String usb,
			String camera, HeDieuHanh heDieuHanh, String cpu, String sms,
			String trinhDuyet, String java, String radio, String music,
			String video, String office, String tinhNangKhac,
			String thoiGianThoai, String thoiGianCho, String phuKienDiKem,
			String noiGiamGia) {
		super(tenSP, giaBan, hangSanXuat, soLuong, hinhAnh, anhDaiDien,
				ngayThang, giaSauKhiGiam, thongTinNoiBat, thoiGianBaoHanh,
				trongLuong, kichThuoc, mauSac);
		this.kichThuocManHinh = kichThuocManHinh;
		this.thongTinManHinh = thongTinManHinh;
		
		this.bangTan = bangTan;
		this.ngonNgu = ngonNgu;
		this.cheDoRung = cheDoRung;
		this.nhacChuong = nhacChuong;
		this.loa = loa;
		this.boNhoTrong = boNhoTrong;
		this.theNhoNgoai = theNhoNgoai;
		this.gprs = gprs;
		this.edge = edge;
		this.g3 = g3;
		this.wifi = wifi;
		this.bluetooth = bluetooth;
		this.hongNgoai = hongNgoai;
		this.usb = usb;
		this.camera = camera;
		this.heDieuHanh = heDieuHanh;
		this.cpu = cpu;
		this.sms = sms;
		this.trinhDuyet = trinhDuyet;
		this.java = java;
		this.radio = radio;
		this.music = music;
		this.video = video;
		this.office = office;
		this.tinhNangKhac = tinhNangKhac;
		this.thoiGianThoai = thoiGianThoai;
		this.thoiGianCho = thoiGianCho;
		this.phuKienDiKem = phuKienDiKem;
		this.noiGiamGia = noiGiamGia;
	}
	public Mobile(int mobileID, String tenSP, int giaBan, String anhDaiDien, int giaSauKhiGiam){
		super(tenSP, giaBan, anhDaiDien, giaSauKhiGiam);
		this.mobileID =mobileID;
		this.tenSP = tenSP;
		this.giaBan = giaBan;
		this.anhDaiDien = anhDaiDien;
		this.giaSauKhiGiam = giaSauKhiGiam;
	}
	
	
	
	
	@Override
	public SanPham[] getMobileByCategory(int cat, boolean limit) {
		// TODO Auto-generated method stub
		ArrayList<Mobile> listMobile = new ArrayList<Mobile>();
        Connection con = ConnectDatabase.getConnection();
        try {
            String sql="select * from mobile where catid=? ORDER BY price DESC";
            if (limit) {
                sql = "select * from mobile where catid=? ORDER BY price DESC limit 0,9 ";
            }
            PreparedStatement sta = con.prepareStatement(sql);
            sta.setInt(1, cat);
            ResultSet re = sta.executeQuery();
            while (re.next()) {
                mobileID = re.getInt("mobileid");
                tenSP = re.getString("name");
                giaBan = re.getInt("price");
                giaSauKhiGiam = re.getInt("discount");

                if (re.wasNull()) {
                    giaSauKhiGiam = giaBan;
                    System.out.println("discount " +giaSauKhiGiam);
                }
                anhDaiDien = re.getString("thumbnail");
                //SanPham mb = new Mobile(mobileID, tenSP, giaBan, giaSauKhiGiam, anhDaiDien);
                // m=(Mobile) mb;
//               listMobile.add(m);
                SanPham sp = new Mobile();
              //  sp.setMobileID(mobileID);
                sp.setTenSP(tenSP);
                sp.setGiaBan(giaBan);
                sp.setanhDaiDien(anhDaiDien);
             Mobile   m=(Mobile) sp;
             m.setMobileID(mobileID);
                listMobile.add(m);
                
                
            }
            con.close();

        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        Mobile[] mobiles = new Mobile[listMobile.size()];
        listMobile.toArray(mobiles);
        return mobiles;
    }
	
	

	@Override
	public Mobile getSanPham(int idSP) {
		// TODO Auto-generated method stub
		Mobile sp = null;
        Connection con = ConnectDatabase.getConnection();
        try {
            PreparedStatement sta = con.prepareStatement("select * from mobile c join operatingsystem os where c.operating_system_id = os.operating_system_id and mobileid=?");
            sta.setInt(1, idSP);
            ResultSet re = sta.executeQuery();
            if (re.next()) {
                 mobileID = re.getInt("mobileid");
                String manufacturerID = re.getString("manufacturer_id");
//                name = re.getString("name");
//                String tenSP = "";
                tenSP = re.getString("name");
                giaBan =re.getInt("price");
                
              //  int price = re.getInt("price");
                //int quantity = re.getInt("quantity");
                soLuong = re.getInt("quantity");
//                String image = re.getString("image");
            //    String hinHanh = re.getString("image");
                hinhAnh = re.getString("image");
                
                anhDaiDien = re.getString("thumbnail");
                giaSauKhiGiam = re.getInt("discount");
                if (re.wasNull()) {
                    giaSauKhiGiam = giaBan;
                }
//                gia = re.getString("discount_place");
//                if (re.wasNull()) {
//                    discountPlace = "";
//                }
                thoiGianBaoHanh = re.getString("warranty");
                phuKienDiKem = re.getString("appendix");
                thongTinNoiBat = re.getString("salient_feature");
                if (re.wasNull()) {
                    thongTinNoiBat = "";
                }
                bangTan = re.getString("waveband");
                if (re.wasNull()) {
                    bangTan = "";
                }
                kichThuoc = re.getString("size");
                if (re.wasNull()) {
                    kichThuoc = "";
                }
                trongLuong= re.getString("weight");
                if (re.wasNull()) {
                    trongLuong = "";
                }
                mauSac = re.getString("color");
                if (re.wasNull()) {
                    mauSac = "";
                }
                kichThuocManHinh = re.getString("resolution");
                if (re.wasNull()) {
                    kichThuocManHinh = "";
                }
                thongTinManHinh = re.getString("screen_info");
                if (re.wasNull()) {
                    thongTinManHinh = "";
                }
                ngonNgu = re.getString("language");
                if (re.wasNull()) {
                    ngonNgu = "";
                }
                cheDoRung = re.getString("shake");
                if (re.wasNull()) {
                    cheDoRung = "";
                }
                nhacChuong = re.getString("ring_tone");
                if (re.wasNull()) {
                    nhacChuong = "";
                }
                loa = re.getString("speaker");
                if (re.wasNull()) {
                    loa = "";
                }
                boNhoTrong = re.getString("internal_memory");
                if (re.wasNull()) {
                    boNhoTrong = "";
                }
                theNhoNgoai = re.getString("external_memory");
                if (re.wasNull()) {
                    theNhoNgoai = "";
                }
                gprs = re.getString("gprs");
                if (re.wasNull()) {
                    gprs = "";
                }
                edge = re.getString("edge");
                if (re.wasNull()) {
                    edge = "";
                }
                g3 = re.getString("3g");
                if (re.wasNull()) {
                    g3 = "";
                }
                wifi = re.getString("wlan");
                if (re.wasNull()) {
                    wifi = "";
                }
                bluetooth = re.getString("bluetooth");
                if (re.wasNull()) {
                    bluetooth = "";
                }
                hongNgoai = re.getString("infra_red");
                if (re.wasNull()) {
                    hongNgoai = "";
                }
                usb = re.getString("usb");
                if (re.wasNull()) {
                    usb = "";
                }
                camera = re.getString("camera");
                if (re.wasNull()) {
                    camera = "";
                }
                String operatingSystemID = re.getString("ten");
                cpu = re.getString("cpu");
                if (re.wasNull()) {
                    cpu = "";
                }
                sms = re.getString("sms");
                if (re.wasNull()) {
                    sms = "";
                }
                trinhDuyet = re.getString("browser");
                if (re.wasNull()) {
                    trinhDuyet = "";
                }
                java = re.getString("java");
                if (re.wasNull()) {
                   java = "";
                }
                radio = re.getString("radio");
                if (re.wasNull()) {
                    radio = "";
                }
                music = re.getString("music");
                if (re.wasNull()) {
                    music = "";
                }
                video = re.getString("video");
                if (re.wasNull()) {
                    video = "";
                }
                office = re.getString("office");
                if (re.wasNull()) {
                    office = "";
                }
                tinhNangKhac = re.getString("others");
                if (re.wasNull()) {
                    tinhNangKhac = "";
                }
                thoiGianCho = re.getString("stand_by_time");
                if (re.wasNull()) {
                    thoiGianCho = "";
                }
                thoiGianThoai = re.getString("talk_time");
                if (re.wasNull()) {
                    thoiGianThoai = "";
                }
                trangThai = re.getString("state");
                if (re.wasNull()) {
                    trangThai = "";
                }
          //      sp = new Mobile(tenSP, giaBan, hangSanXuat, soLuong, hinhAnh, ngayThang, giamSauKhiGiam, thongTinNoiBat, thoiGianBaoHanh, trongLuong, kichThuoc, mauSac, mobileID);
            //    System.out.println("sp " + sp);
                sp = new Mobile();
                sp.setMobileID(mobileID);
                sp.setTenSP(tenSP);
                sp.setCamera(camera);
                sp.setanhDaiDien(anhDaiDien);
                sp.setBangTan(bangTan);
                sp.setBluetooth(bluetooth);
                sp.setBoNhoTrong(boNhoTrong);
                sp.setCheDoRung(cheDoRung);
                sp.setCpu(cpu);
                sp.setEdge(edge);
                sp.setG3(g3);
                sp.setGiaBan(giaBan);
                sp.setGiaSauKhiGiam(giaSauKhiGiam);
                sp.setGprs(gprs);
                sp.setHangSanXuat(hangSanXuat);              
                sp.setHeDieuHanh(new HeDieuHanh(operatingSystemID));
                sp.setHinhAnh(hinhAnh);
                sp.setHongNgoai(hongNgoai);
                sp.setJava(java);
                sp.setKichThuoc(kichThuoc);
                sp.setKichThuocManHinh(kichThuocManHinh);
                sp.setLoa(loa);
                sp.setMauSac(mauSac);
                
                sp.setMusic(music);
                sp.setNgayThang(ngayThang);
                sp.setNgonNgu(ngonNgu);
                sp.setNhacChuong(nhacChuong);
                sp.setOffice(office);
                sp.setPhuKienDiKem(phuKienDiKem);
                sp.setRadio(radio);
                sp.setSms(sms);
                sp.setSoLuong(soLuong);
                sp.setTheNhoNgoai(theNhoNgoai);
                sp.setThoiGianBaoHanh(thoiGianBaoHanh);
                sp.setThoiGianCho(thoiGianCho);
                sp.setThoiGianThoai(thoiGianThoai);
                sp.setThongTinManHinh(thongTinManHinh);
                sp.setThongTinNoiBat(thongTinNoiBat);
                sp.setTinhNangKhac(tinhNangKhac);
                sp.setTrinhDuyet(trinhDuyet);
                sp.setTrangThai(trangThai);
                sp.setTrongLuong(trongLuong);
                sp.setUsb(usb);
                sp.setVideo(video);
                sp.setWifi(wifi);
                
                
                
            }
            con.close();

        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return sp;
	}
	
	 public  double getTotal(){
	        double total = this.giaBan*this.soLuong;
	        return total;
	    }

	 
	 public String getTotalCurrencyFormat(){
	        NumberFormat currency = NumberFormat.getInstance();
	        return currency.format(this.getTotal());
	    }
	public int getMobileID() {
		return mobileID;
	}

	public void setMobileID(int mobileID) {
		this.mobileID = mobileID;
	}

	public String getKichThuocManHinh() {
		return kichThuocManHinh;
	}

	public void setKichThuocManHinh(String kichThuocManHinh) {
		this.kichThuocManHinh = kichThuocManHinh;
	}

	public String getThongTinManHinh() {
		return thongTinManHinh;
	}

	public void setThongTinManHinh(String thongTinManHinh) {
		this.thongTinManHinh = thongTinManHinh;
	}


	public String getBangTan() {
		return bangTan;
	}

	public void setBangTan(String bangTan) {
		this.bangTan = bangTan;
	}

	public String getNgonNgu() {
		return ngonNgu;
	}

	public void setNgonNgu(String ngonNgu) {
		this.ngonNgu = ngonNgu;
	}

	public String getCheDoRung() {
		return cheDoRung;
	}

	public void setCheDoRung(String cheDoRung) {
		this.cheDoRung = cheDoRung;
	}

	public String getNhacChuong() {
		return nhacChuong;
	}

	public void setNhacChuong(String nhacChuong) {
		this.nhacChuong = nhacChuong;
	}

	public String getLoa() {
		return loa;
	}

	public void setLoa(String loa) {
		this.loa = loa;
	}

	public String getBoNhoTrong() {
		return boNhoTrong;
	}

	public void setBoNhoTrong(String boNhoTrong) {
		this.boNhoTrong = boNhoTrong;
	}

	public String getTheNhoNgoai() {
		return theNhoNgoai;
	}

	public void setTheNhoNgoai(String theNhoNgoai) {
		this.theNhoNgoai = theNhoNgoai;
	}

	public String getGprs() {
		return gprs;
	}

	public void setGprs(String gprs) {
		this.gprs = gprs;
	}

	public String getEdge() {
		return edge;
	}

	public void setEdge(String edge) {
		this.edge = edge;
	}

	public String getG3() {
		return g3;
	}

	public void setG3(String g3) {
		this.g3 = g3;
	}

	public String getWifi() {
		return wifi;
	}

	public void setWifi(String wifi) {
		this.wifi = wifi;
	}

	public String getBluetooth() {
		return bluetooth;
	}

	public void setBluetooth(String bluetooth) {
		this.bluetooth = bluetooth;
	}

	public String getHongNgoai() {
		return hongNgoai;
	}

	public void setHongNgoai(String hongNgoai) {
		this.hongNgoai = hongNgoai;
	}

	public String getUsb() {
		return usb;
	}

	public void setUsb(String usb) {
		this.usb = usb;
	}

	public String getCamera() {
		return camera;
	}

	public void setCamera(String camera) {
		this.camera = camera;
	}

	public HeDieuHanh getHeDieuHanh() {
		return heDieuHanh;
	}

	public void setHeDieuHanh(HeDieuHanh heDieuHanh) {
		this.heDieuHanh = heDieuHanh;
	}

	public String getCpu() {
		return cpu;
	}

	public void setCpu(String cpu) {
		this.cpu = cpu;
	}

	public String getSms() {
		return sms;
	}

	public void setSms(String sms) {
		this.sms = sms;
	}

	public String getTrinhDuyet() {
		return trinhDuyet;
	}

	public void setTrinhDuyet(String trinhDuyet) {
		this.trinhDuyet = trinhDuyet;
	}

	public String getJava() {
		return java;
	}

	public void setJava(String java) {
		this.java = java;
	}

	public String getRadio() {
		return radio;
	}

	public void setRadio(String radio) {
		this.radio = radio;
	}

	public String getMusic() {
		return music;
	}

	public void setMusic(String music) {
		this.music = music;
	}

	public String getVideo() {
		return video;
	}

	public void setVideo(String video) {
		this.video = video;
	}

	public String getOffice() {
		return office;
	}

	public void setOffice(String office) {
		this.office = office;
	}
	public String getAnhDaiDien() {
		return anhDaiDien;
	}

	public void setAnhDaiDien(String anhDaiDien) {
		this.anhDaiDien = anhDaiDien;
	}

	public String getTinhNangKhac() {
		return tinhNangKhac;
	}

	public void setTinhNangKhac(String tinhNangKhac) {
		this.tinhNangKhac = tinhNangKhac;
	}
	public String formatedPrice() {
        NumberFormat nf = NumberFormat.getInstance();
        return nf.format(this.giaBan);
        
    }
	  public String formatedDiscount() {
	        NumberFormat nf = NumberFormat.getInstance();
	        return nf.format(this.giaSauKhiGiam);
	    }

	public String getThoiGianThoai() {
		return thoiGianThoai;
	}

	public void setThoiGianThoai(String thoiGianThoai) {
		this.thoiGianThoai = thoiGianThoai;
	}

	public String getThoiGianCho() {
		return thoiGianCho;
	}

	public void setThoiGianCho(String thoiGianCho) {
		this.thoiGianCho = thoiGianCho;
	}

	public String getPhuKienDiKem() {
		return phuKienDiKem;
	}

	public void setPhuKienDiKem(String phuKienDiKem) {
		this.phuKienDiKem = phuKienDiKem;
	}

	public String getNoiGiamGia() {
		return noiGiamGia;
	}

	public void setNoiGiamGia(String noiGiamGia) {
		this.noiGiamGia = noiGiamGia;
	}

	public String getTrangThai() {
		return trangThai;
	}

	public void setTrangThai(String trangThai) {
		this.trangThai = trangThai;
	}
	// Begin ADMIN METHOD
	
	 public static ArrayList<Mobile> searchMobile(String keyword, String manufacturer_id, int page) {
	        manufacturer_id = (manufacturer_id.equals("")) ? "%" : manufacturer_id;

	        ArrayList<Mobile> listMobile2 = new ArrayList<Mobile>();
	        Connection con = ConnectDatabase.getConnection();
	        try {
	            Statement sta = con.createStatement();
	            String sql = "select * from mobile where manufacturer_id like'" + manufacturer_id + "' and name like'%" + keyword + "%' ORDER BY mobileid DESC LIMIT 15 OFFSET " + (page - 1) * 15;
	            ResultSet re = sta.executeQuery(sql);
	            while (re.next()) {
	                String mobileID1 = re.getString("mobileid");
	                String name1 = re.getString("name");
	                int price = re.getInt("price");
	                int quantity = re.getInt("quantity");
	                String thumbnail = re.getString("thumbnail");
	           //     Mobile mb = new Mobile(mobileID, name, price, thumbnail, quantity);
	                Mobile mb=new Mobile();
	                mb.setMobileID(Integer.parseInt(mobileID1));
	                mb.setTenSP(name1);
	                mb.setGiaBan(price);
	                mb.setAnhDaiDien(thumbnail);
	                mb.setSoLuong(quantity);		                
	                
	                listMobile2.add(mb);

	            }
	            con.close();
	        } catch (SQLException ex) {
	            ex.printStackTrace();
	        }
	        return listMobile2;
	    }
	 public  void addMobile11(Mobile m,int cat) {
			//Mobile m=(Mobile) sp;
			HangSanXuat hsx;
			String manufacturerID,  name,  price,
	        quantity, image,  thumbnail,  date,  discount,
	         discountPlace,  warranty, appendix,  salientFeature,
	         waveband,  size,  weight,  color,  resolution,
	         screenInfo,  language,  shake,  ringTone,  speaker,
	         internalMemory,  externalMemory,  gprs,
	         edge,  _3g,  wlan,  bluetooth,  infraRed,
	         usb,  camera,  operatingSystemID,  cpu,  sms,
	         browser,  java,  radio,  music,  video,
	         office,  others,  standByTime,  talkTime,  state,  catId;
			
			Connection con = ConnectDatabase.getConnection();
	        try {
	        	manufacturerID=m.getHangSanXuat().getTenNhaSanXuat();
	        	name=m.getTenSP();
	      int  price1=m.getGiaBan();
	        int	quantity1=m.getSoLuong();
	        	image=m.getHinhAnh();
	        	thumbnail=m.getAnhDaiDien();
	        java.util.Date	date1=m.getNgayThang();
	        int	discount1=m.getGiaSauKhiGiam();
	        discountPlace="";
	        warranty=m.getThoiGianBaoHanh();
	        appendix=m.getPhuKienDiKem();
	        salientFeature=m.getThongTinNoiBat();
	        waveband=m.getBangTan();
	        size=m.getKichThuoc();
	        weight=m.getTrongLuong();
	        color=m.getMauSac();
	        resolution=m.getKichThuocManHinh();
	        screenInfo=m.getThongTinManHinh();
	        language=m.getNgonNgu();
	        shake=m.getCheDoRung();
	        ringTone=m.getNhacChuong();
	        speaker=m.getLoa();
	        internalMemory=m.getBoNhoTrong();
	        externalMemory=m.getTheNhoNgoai();
	        gprs=m.getGprs();
	        edge=m.getEdge();
	        _3g=m.getG3();
	        wlan=m.getWifi();
	        bluetooth=m.getBluetooth();
	        infraRed=m.getHongNgoai();
	        usb=m.getUsb();
	        camera=m.getCamera();
	        HeDieuHanh hdh=m.getHeDieuHanh();
	        operatingSystemID=hdh.getTenHeDieuHanh();
	        cpu=m.getCpu();
	        sms=m.getSms();
	        browser=m.getTrinhDuyet();
	        java=m.getJava();
	        radio=m.getRadio();
	        music=m.getMusic();
	        video=m.getVideo();
	        office=m.getOffice();
	        others=m.getTinhNangKhac();
	        standByTime=m.getThoiGianCho();
	        talkTime=m.getThoiGianThoai();
	        state=m.getTrangThai();
	        
	        	
	        	
	        	
	        	
	            Statement sta = con.createStatement();
	            String sql = "INSERT INTO mobile(manufacturer_id, name, price, "
	                    + "quantity,image, thumbnail, date, discount, "
	                    + "discount_place,warranty, appendix, salient_feature, "
	                    + "waveband, size, weight,color, resolution, screen_info, "
	                    + "language, shake,ring_tone, speaker, internal_memory, "
	                    + "external_memory, gprs,edge, 3g, wlan, bluetooth, infra_red, "
	                    + "usb, camera,operating_system_id, cpu, sms, browser, java,"
	                    + "radio, music, video, office, others, stand_by_time, "
	                    + "talk_time, state,catid) VALUES(" + manufacturerID + ","
	                    + "'" + name + "',"
	                    + price1 + ","
	                    + quantity1 + ","
	                    + "'" + image + "',"
	                    + "'" + thumbnail + "',"
	                    + "'" + date1 + "',"
	                    + discount1 + ","
	                    + "'" + discountPlace + "',"
	                    + "'" + warranty + "',"
	                    + "'" + appendix + "',"
	                    + "'" + salientFeature + "',"
	                    + "'" + waveband + "',"
	                    + "'" + size + "',"
	                    + "'" + weight + "',"
	                    + "'" + color + "',"
	                    + "'" + resolution + "',"
	                    + "'" + screenInfo + "',"
	                    + "'" + language + "',"
	                    + "'" + shake + "',"
	                    + "'" + ringTone + "',"
	                    + "'" + speaker + "',"
	                    + "'" + internalMemory + "',"
	                    + "'" + externalMemory + "',"
	                    + "'" + gprs + "',"
	                    + "'" + edge + "',"
	                    + "'" + _3g + "',"
	                    + "'" + wlan + "',"
	                    + "'" + bluetooth + "',"
	                    + "'" + infraRed + "',"
	                    + "'" + usb + "',"
	                    + "'" + camera + "',"
	                    + "'" + operatingSystemID + "',"
	                    + "'" + cpu + "',"
	                    + "'" + sms + "',"
	                    + "'" + browser + "',"
	                    + "'" + java + "',"
	                    + "'" + radio + "',"
	                    + "'" + music + "',"
	                    + "'" + video + "',"
	                    + "'" + office + "',"
	                    + "'" + others + "',"
	                    + "'" + standByTime + "',"
	                    + "'" + talkTime + "',"
	                    + "'" + state + "',"
	                    + "'" + cat + "')";
	            sta.executeUpdate(sql);
	            con.close();
	        } catch (SQLException ex) {
	            ex.printStackTrace();
	        }
		
			
			// TODO Auto-generated method stub
			
		}
	 
	 @Override
		public void deleteMobile(int mobileID) {
			
			// TODO Auto-generated method stub
			
			 Connection con = ConnectDatabase.getConnection();
		        try {
		            Statement sta = con.createStatement();
		            String sql = "DELETE FROM mobile WHERE mobileid = " + mobileID;
		            sta.executeUpdate(sql);
		            con.close();
		        } catch (SQLException ex) {
		            ex.printStackTrace();
		        }
			
		}
		
	    public static String getMobileImage(String mobileid) {
	        String result = "";
	        Connection con = ConnectDatabase.getConnection();
	        try {
	            Statement sta = con.createStatement();
	            String sql = "SELECT image FROM mobile WHERE mobileid = " + mobileid;
	            ResultSet re = sta.executeQuery(sql);
	            if (re.next()) {
	                result = re.getString(1);
	            }
	            con.close();
	        } catch (SQLException ex) {
	            ex.printStackTrace();
	        }
	        return result;
	    }

	    public static String getMobileThumbnail(String mobileid) {
	        String result = "";
	        Connection con = ConnectDatabase.getConnection();
	        try {
	            Statement sta = con.createStatement();
	            String sql = "SELECT thumbnail FROM mobile WHERE mobileid = " + mobileid;
	            ResultSet re = sta.executeQuery(sql);
	            if (re.next()) {
	                result = re.getString(1);
	            }
	            con.close();
	        } catch (SQLException ex) {
	            ex.printStackTrace();
	        }
	        return result;
	    }
	    public static int countRecord(String keyword, String manufacturer_id) {
	        manufacturer_id = (manufacturer_id.equals("")) ? "%" : manufacturer_id;
	        int result = 0;
	        Connection con = ConnectDatabase.getConnection();
	        try {
	            Statement sta = con.createStatement();
	            String sql = "select count(*) from mobile where manufacturer_id like'" + manufacturer_id + "' and name like'%" + keyword + "%'";
	            ResultSet re = sta.executeQuery(sql);
	            re.next();
	            result = re.getInt(1);
	            con.close();
	        } catch (SQLException ex) {
	            ex.printStackTrace();
	        }
	        return result;
	    }




	
	
	
	
	public static void main(String[] args) {
   	 Mobile mobile = new Mobile();
   	Mobile m = mobile.getSanPham(14);
		System.out.println("edsdsds "+ m.getTenSP()+m.getCamera()+m.getBangTan()+m.getG3()+m.getGiaBan()+ m.giaSauKhiGiam+m.getHeDieuHanh().toString());
	}

	public ArrayList<SanPham> getMobileByManufacturer(int producerID) {
		ArrayList<SanPham> listMobile = new ArrayList<SanPham>();
        Connection con = ConnectDatabase.getConnection();
        try {
            PreparedStatement sta = con.prepareStatement("select * from mobile where manufacturer_id=?");
            sta.setInt(1, producerID);
            ResultSet re = sta.executeQuery();
            while (re.next()) {
                mobileID = re.getInt("mobileid");
                tenSP = re.getString("name");
                giaBan = re.getInt("price");
                giaSauKhiGiam = re.getInt("discount");
                if (re.wasNull()) {
                    giaSauKhiGiam = giaBan;
                }
                anhDaiDien = re.getString("thumbnail");
                SanPham mb = new Mobile(mobileID, tenSP, giaBan, anhDaiDien, giaSauKhiGiam);
                listMobile.add(mb);
            }
            con.close();

        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return listMobile;
		
		
	}
	public  void updateMobile(Mobile m,int cat) {
		//Mobile m=(Mobile) sp;
		HangSanXuat hsx;
		String manufacturerID,  name,  price,
        quantity, image,  thumbnail,  date,  discount,
         discountPlace,  warranty, appendix,  salientFeature,
         waveband,  size,  weight,  color,  resolution,
         screenInfo,  language,  shake,  ringTone,  speaker,
         internalMemory,  externalMemory,  gprs,
         edge,  _3g,  wlan,  bluetooth,  infraRed,
         usb,  camera,  operatingSystemID,  cpu,  sms,
         browser,  java,  radio,  music,  video,
         office,  others,  standByTime,  talkTime,  state,  catId;
	
		Connection con = ConnectDatabase.getConnection();
        try {
        	manufacturerID=m.getHangSanXuat().getTenNhaSanXuat();
        	int mobileid=m.getMobileID();
        	name=m.getTenSP();
      int  price1=m.getGiaBan();
        int	quantity1=m.getSoLuong();
        	image=m.getHinhAnh();
        	thumbnail=m.getAnhDaiDien();
        java.util.Date	date1=m.getNgayThang();
        int	discount1=m.getGiaSauKhiGiam();
        discountPlace="";
        warranty=m.getThoiGianBaoHanh();
        appendix=m.getPhuKienDiKem();
        salientFeature=m.getThongTinNoiBat();
        waveband=m.getBangTan();
        size=m.getKichThuoc();
        weight=m.getTrongLuong();
        color=m.getMauSac();
        resolution=m.getKichThuocManHinh();
        screenInfo=m.getThongTinManHinh();
        language=m.getNgonNgu();
        shake=m.getCheDoRung();
        ringTone=m.getNhacChuong();
        speaker=m.getLoa();
        internalMemory=m.getBoNhoTrong();
        externalMemory=m.getTheNhoNgoai();
        gprs=m.getGprs();
        edge=m.getEdge();
        _3g=m.getG3();
        wlan=m.getWifi();
        bluetooth=m.getBluetooth();
        infraRed=m.getHongNgoai();
        usb=m.getUsb();
        camera=m.getCamera();
        HeDieuHanh hdh=m.getHeDieuHanh();
        operatingSystemID=hdh.getTenHeDieuHanh();
        cpu=m.getCpu();
        sms=m.getSms();
        browser=m.getTrinhDuyet();
        java=m.getJava();
        radio=m.getRadio();
        music=m.getMusic();
        video=m.getVideo();
        office=m.getOffice();
        others=m.getTinhNangKhac();
        standByTime=m.getThoiGianCho();
        talkTime=m.getThoiGianThoai();
        state=m.getTrangThai();
        
        	
        	
        	
        	
            Statement sta = con.createStatement();
            String sql1 = "DELETE FROM mobile WHERE mobileid = " + mobileid;
            String sql = "INSERT INTO mobile(manufacturer_id, name, price, "
                    + "quantity,image, thumbnail, date, discount, "
                    + "discount_place,warranty, appendix, salient_feature, "
                    + "waveband, size, weight,color, resolution, screen_info, "
                    + "language, shake,ring_tone, speaker, internal_memory, "
                    + "external_memory, gprs,edge, 3g, wlan, bluetooth, infra_red, "
                    + "usb, camera,operating_system_id, cpu, sms, browser, java,"
                    + "radio, music, video, office, others, stand_by_time, "
                    + "talk_time, state,catid) VALUES(" + manufacturerID + ","
                    + "'" + name + "',"
                    + price1 + ","
                    + quantity1 + ","
                    + "'" + image + "',"
                    + "'" + thumbnail + "',"
                    + "'" + date1 + "',"
                    + discount1 + ","
                    + "'" + discountPlace + "',"
                    + "'" + warranty + "',"
                    + "'" + appendix + "',"
                    + "'" + salientFeature + "',"
                    + "'" + waveband + "',"
                    + "'" + size + "',"
                    + "'" + weight + "',"
                    + "'" + color + "',"
                    + "'" + resolution + "',"
                    + "'" + screenInfo + "',"
                    + "'" + language + "',"
                    + "'" + shake + "',"
                    + "'" + ringTone + "',"
                    + "'" + speaker + "',"
                    + "'" + internalMemory + "',"
                    + "'" + externalMemory + "',"
                    + "'" + gprs + "',"
                    + "'" + edge + "',"
                    + "'" + _3g + "',"
                    + "'" + wlan + "',"
                    + "'" + bluetooth + "',"
                    + "'" + infraRed + "',"
                    + "'" + usb + "',"
                    + "'" + camera + "',"
                    + "'" + operatingSystemID + "',"
                    + "'" + cpu + "',"
                    + "'" + sms + "',"
                    + "'" + browser + "',"
                    + "'" + java + "',"
                    + "'" + radio + "',"
                    + "'" + music + "',"
                    + "'" + video + "',"
                    + "'" + office + "',"
                    + "'" + others + "',"
                    + "'" + standByTime + "',"
                    + "'" + talkTime + "',"
                    + "'" + state + "',"
                    + "'" + cat + "')";
         //   sta.executeUpdate(sql1);
            sta.executeUpdate(sql);
            con.close();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
	
		
		// TODO Auto-generated method stub
		
	}
    
	
	
	
	
	public int getCatId(int mobileID) {
		
		 int result =0;
	        Connection con = ConnectDatabase.getConnection();
	        try {
	            Statement sta = con.createStatement();
	            String sql = "SELECT catid FROM mobile WHERE mobileid = " + mobileID;
	            ResultSet re = sta.executeQuery(sql);
	            if (re.next()) {
	                result = re.getInt(1);
	            }
	            con.close();
	        } catch (SQLException ex) {
	            ex.printStackTrace();
	        }
	        return result;
		
	}

}
